'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:PersonnelTcsqtipCtrl
 * @description
 * # PersonnelTcsqtipCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('PersonnelTcsqtipCtrl', function ($scope, $rootScope, openApi, $http, Util, $location, $routeParams, $filter, localStorageService, FormSubmit) {
        $scope.sheetObj = {};
        // 底部关联模块类型选框添加
        var relModuleObj = {},
            selectData, params;
        relModuleObj.sheettitle = "关联模块";
        relModuleObj.callback = function (item) {
            if (item.selectTabContent == "无") {
                $scope.param.relatedModuleName = "无";
                $scope.param.relatedModule = "";
                $scope.param.moduleNo = "";
                return;
            }
            $scope.param.relatedModule = item.selectTab;
            $scope.param.relatedModuleName = item.selectTabContent;
            $scope.param.moduleNo = ""

        }
        $scope.sheetObj.relModuleObj = relModuleObj;
        $scope.showrelModule = function () {
            Util.getModuleRel(Util.TCSQ_MODULE, function (data) {
                $scope.sheetObj.relModuleObj.tabList = data.data
                $scope.showSheet("relModuleObj")
            })

        };
        $scope.showSheet = function (type) {
            var sheetObj = $scope.sheetObj[type]
            $scope.sheetObj.type = type
            $scope.sheetObj.tabList = sheetObj.tabList
            $scope.sheetObj.sheettitle = sheetObj.sheettitle
            $scope.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true
        };
        $scope.chooseinfo = function () {
            // $routeParams.id通过id确定唯一回显id
            if (!$scope.param.relatedModule) {
                return
            }
            Util.savaParamForReview($scope.param, $routeParams.id);
            // 里面包含了title
            var relModuleObj = {
                name: "PersonnelTcsqtipCtrl",
                selectTab: $scope.param.relatedModule,
                selectTabContent: $scope.param.relatedModuleName
            };
            Util.savaDataforsearch(relModuleObj);
            $location.path("finance/choosepeople")

        };
        $scope.submit = function () {
            // 底部类型end
            if ($scope.param.relatedModuleName && $scope.param.relatedModuleName !== '无') {
                if (!$scope.param.moduleNo) {
                    Util.Toast("请选择模块编号");
                    return;
                }
            }
            if (!$scope.param.year) {
                Util.Toast("请填写提成年份");
                return;
            }
            if (!$scope.param.month) {
                Util.Toast("请填写提成月份");
                return;
            }
            if (!$scope.param.bidIncome) {
                Util.Toast("请填写收入总额");
                return;
            }
            if ($scope.param.formula) {
                if ($scope.param.formula > 100) {
                    Util.Toast("提成比例应该小于100");
                    return;
                }
            } else {
                Util.Toast("请填写提成比例");
                return
            }
            if (!$scope.param.commisionAmount) {
                Util.Toast("请填写提成奖励金额");
                return;
            }

            // 提交
            var TVtype;
            TVtype = Util.getTVType(); //根据页面的来源来判断是发起的流程还是修改的流程，如果是发起的则不管，如果是修改的则获取数据用来回显
            var urlParam = "?operationId=-2" + "&&processId=" + $routeParams.id + "&&url=" + openApi.commisionApply
            var options = {};
            options.urlParam = urlParam;
            options.param = $scope.param;
            // 如果是修改页面的话则提交的地址不一样，参数一样
            if (TVtype == Util.UPDATE) {
                options.url = openApi.ip + openApi.commisionApply
            }
            new FormSubmit(options);
        };
        // 初始化逻辑,如果是修改页面的话则先从接口请求数据回显，再将缓存中的跳转来源删除，方便页面跳转返回之后的回显
        var init = function () {
            // 获取回显的详细信息
            Util.getDetailData($routeParams.id, function (data) {
                $scope.param = data;
            });
            // 显示审批记录判断
            Util.showloglist($routeParams.id, function (data) {
                $scope.logData = data;
            });
            // 初始化当前5年的数组
            initYears(5);
            console.log($scope.years)
        };
        // 初始化5年
        var initYears = function (len) {
            $scope.years = [];
            var date = new Date;
            var year = date.getFullYear();
            while (len > 0) {
                len--;
                $scope.years.push(year+"年");
                year--
            }
        };
        $scope.months=[1,2,3,4,5,6,7,8,9,10,11,12].map(function(item,index){
            return item+"月"
        })
        // 初始化
        init();
    });
